#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Created on 18-1-2
@author: yzw
@desc:
"""
import json

from django.http import HttpResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt

from blog_yzw.blog_app.models import *
from blog_yzw.blog_app.utils import get_id


# 跳转博客列表
def blog_list(request):
	return render(request, 'blog/blog_list.html')


# 跳转博客分类
def blog_classify(request):
	return render(request, 'blog/blog_classify.html')


# 添加博客分类
@csrf_exempt
def add_classify(request):
	if 'classify' in request.POST:
		classify_name = request.POST['classify']
		obj = Classify.objects.filter(name=classify_name)
		if obj.exists():
			return HttpResponse('{"msg": "新增失败，分类已存在"}', content_type="application/json")
		else:
			Classify.objects.create(id=get_id(), name=classify_name, status='1')
			return HttpResponse('{"msg": "新增成功"}', content_type="application/json")
	else:
		return HttpResponse('{"msg": "新增失败，参数异常"}', content_type="application/json")


# 加载博客分类:下拉框内容
@csrf_exempt
def load_blog_classify(request):
	obj = Classify.objects.filter(status='1')
	# 定义一个空字典
	data_dict = {}
	if obj.exists():
		for classify in obj:
			data_dict[classify.id] = classify.name
	return HttpResponse(json.dumps(data_dict), content_type='application/json')


# 加载博客分类:表格内容
@csrf_exempt
def load_classify_table_data(request):
	result_dict = {}
	data_list = []
	obj_list = Classify.objects.all()
	if 'classify_name' in request.POST:
		classify_name = request.POST['classify_name']
		if classify_name != '':
			obj_list = obj_list.filter(name__icontains=classify_name)

	count = obj_list.count()
	# 限制返回数:分页
	if 'start' and 'limit' in request.POST:
		start = int(request.POST['start'])
		limit = int(request.POST['limit'])
		limit = start + limit
		obj_list = obj_list[start:limit]
	if obj_list.exists():
		for obj in obj_list:
			data_dict = {}
			if obj.status == '1':
				status = '有效'
			else:
				status = '失效'
			data_dict['classify_id'] = obj.id
			data_dict['classify_name'] = obj.name
			data_dict['status'] = status
			data_dict['createTime'] = obj.create_time.strftime('%Y-%m-%d %H:%M:%S')
			data_list.append(data_dict)
		result_dict['data'] = data_list
		result_dict['code'] = 0
		result_dict['count'] = count
	return HttpResponse(json.dumps(result_dict), content_type="application/json")


# 新增博客
@csrf_exempt
def add_blog_content(request):
	if 'phone' in request.session and 'name' in request.session:
		author = request.session.get('name')
	else:
		return render(request, 'login.html')
	if 'title' and 'content' and 'classify' and 'status' in request.POST:
		title = request.POST['title']
		content = request.POST['content']
		classify_id = request.POST['classify']
		status = request.POST['status']
		Article.objects.create(id=get_id(), title=title, content=content, author=author, status=status,
		                       classify_id=classify_id)
		return HttpResponse('{"msg": "博客新增成功"}', content_type="application/json")
	else:
		return HttpResponse('{"msg": "新增失败，参数异常"}', content_type="application/json")


# 加载博客数据
@csrf_exempt
def load_blog_data(request):
	result_dict = {}
	data_list = []
	obj_list = Article.objects.all()
	if 'title' in request.POST:
		title = request.POST['title']
		if title != '':
			# 模糊查询
			obj_list = obj_list.filter(title__icontains=title)
	if 'classify_id' in request.POST:
		classify_id = request.POST['classify_id']
		if classify_id != '':
			obj_list = obj_list.filter(classify_id=classify_id)
	count = obj_list.count()
	# 限制返回数:分页
	if 'start' and 'limit' in request.POST:
		start = int(request.POST['start'])
		limit = int(request.POST['limit'])
		limit = start + limit
		obj_list = obj_list[start:limit]
	if obj_list.exists():
		for obj in obj_list:
			data_dict = {}
			name = Classify.objects.filter(id=obj.classify_id).values('name')[0:1][0]['name']
			data_dict['title'] = obj.title
			data_dict['createTime'] = obj.create_time.strftime('%Y-%m-%d %H:%M:%S')
			data_dict['author'] = obj.author
			data_dict['content'] = obj.content
			data_dict['blog_id'] = obj.id
			data_dict['classify'] = name
			data_dict['classify_id'] = obj.classify_id
			pv_obj = BlogPv.objects.filter(id=obj.id)[0:1]
			if pv_obj.exists():
				pv_num = pv_obj[0].pv_num
			else:
				pv_num = 0
			data_dict['pvNum'] = pv_num
			if '1' == obj.status:
				data_dict['status'] = '公开'
			else:
				data_dict['status'] = '草稿'
			data_list.append(data_dict)
		result_dict['data'] = data_list
		result_dict['code'] = 0
		result_dict['count'] = count
	return HttpResponse(json.dumps(result_dict), content_type="application/json")


# 删除博客
@csrf_exempt
def del_blog(request):
	if 'blog_id' in request.POST:
		blog_id = request.POST['blog_id']
		Article.objects.get(id=blog_id).delete()
		return HttpResponse('{"msg": "删除成功"}', content_type="application/json")
	else:
		return HttpResponse('{"msg": "删除失败，参数异常"}', content_type="application/json")


# 删除博客分类
@csrf_exempt
def del_classify(request):
	if 'classify_id' in request.POST:
		classify_id = request.POST['classify_id']
		blog_obj = Article.objects.filter(classify_id=classify_id)
		if blog_obj.exists():
			return HttpResponse('{"msg": "删除失败，此分类被占用"}', content_type="application/json")
		Classify.objects.get(id=classify_id).delete()
		return HttpResponse('{"msg": "删除成功"}', content_type="application/json")
	else:
		return HttpResponse('{"msg": "删除失败，参数异常"}', content_type="application/json")
